﻿$(document).ready(function ()
{
    var p = $(".post-comment > h3");
    var reply = false;
    var validEmail = false;
        
    $('#email').blur(function () {
        if ($(this).val() != '') {
            var pattern = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
            if (pattern.test($(this).val())) {
                $('#valid').text('Correct').css({ 'color': '#569b44' });
                validEmail = true;
            } else {
                $('#valid').text('Incorrect Email').css({ 'color': '#ff0000' });
                validEmail = false;
            }
        } else {
            $('#valid').text('Email field must not be empty').css({ 'color': '#ff0000' });
            validEmail = false;
        }
    });

    function reply_(e, obj)
    {
        e.preventDefault();
        var replyToId = obj.attr("rel").replace("id_", "");

        $(".post-comment > h3").html("Reply to<a id='cancel' href='#' onclick='javascript:void(0);' style='padding-left: 20px;'>Cancel</a>").attr("class", replyToId);
        var message = obj.parent().siblings("p").text();
        $("#quoting").text('"' + message + '"');
        document.location.href = "#post-comment";
            
        reply = true;
    }
       
    $(".reply").click(function (e){reply_(e, $(this));});

    $(document).on("click", "#cancel", function(e) {
        reply = false;
        $(".post-comment > h3").html("Post Comment");
        e.preventDefault();
        $("#quoting").text("");
    });

    $("#post").click(function (event) {
        var r;
        var p = $(".post-comment > h3");

        if (reply == true)
            r = p.attr("class");
        else
            r = null;

        var data = {
            entryId: p.attr("id"),
            name: $("#name").val(),
            email: $("#email").val(),
            comment: $("#message").val(),
            replyId: r
        };

        if (validEmail == true) {
            $.ajax({
                dataType: "json",
                type: "POST",
                url: '/tools/addpagecomment',
                data: {
                    entryId: p.attr("id"),
                    name: $("#name").val(),
                    email: $("#email").val(),
                    comment: $("#message").val(),
                    replyId: r
                },
                async: false,
                success: function(message) {
                    if (message.result) {

                        if (data.replyId != null) {
                            var mes = "<div class='media' id='temp'>" +
                                "<hr><a class='pull-left' href='#'><img class='media-object' src='http://www.gravatar.com/avatar/" +
                                message.hash
                                + "?d=monsterid' alt=''>" +
                                "</a><div class='media-body'><h4 class='media-heading'>" + data.name + "</h4><span>9/3/2013 2:28 PM | " +
                                "<a class='reply' rel='id_" + message.id + "' href='#'>Reply</a></span><p>" + data.comment + "</p></div></div>";

                            $("a[rel $= '" + data.replyId + "']").parent().parent().children("p:last").after(mes);
                            $("#temp").hide().fadeIn(900).attr("id", "");

                            $(".reply").click(function (e){reply_(e, $(this));});

                            event.preventDefault ? event.preventDefault() : (event.returnValue = false);
                        }
                        else {
                            var mes = "<div class='media' id='temp'>" +
                                "<hr><a class='pull-left' href='#'><img class='media-object' src='http://www.gravatar.com/avatar/" +
                                message.hash
                                + "?d=monsterid' alt=''>" +
                                "</a><div class='media-body'><h4 class='media-heading'>" + data.name + "</h4><span>9/3/2013 2:28 PM | " +
                                "<a class='reply' rel='id_" + message.id + "' href='#'>Reply</a></span><p>" + data.comment + "</p></div></div>";

                            $(".span12").children("hr:last").before(mes);
                            $("#temp").hide().fadeIn(900).attr("id", "");

                            $(".reply").click(function (e){reply_(e, $(this));});

                            event.preventDefault ? event.preventDefault() : (event.returnValue = false);
                        }
                    }
                },
                error: function() {
                    alert("ERROR");
                }
            });

            $('#myform').trigger('reset');
            $('#valid').text('');
            validEmail = false;

        }
        else {
            $('input#email').focus();
            event.preventDefault ? event.preventDefault() : (event.returnValue = false);
        }
    });
})